package com.example.roles_management_p1.controller;

import com.example.roles_management_p1.service.IRolesService;
import com.pojo.Entity.Roles;
import com.pojo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/roles")
public class RolesController extends  BaseController{
    @Autowired
    private IRolesService rolesService;
    @PostMapping("/check")
    public Result<?> check(HttpServletRequest httpServletRequest){
        Integer count=rolesService.isexist(httpServletRequest);
        System.out.println(count);
        return count==null?Result.failure():Result.success(count);
    }
    @PostMapping("/regiest")
    public Result<?>regiest( Roles role, HttpServletRequest httpServletRequest){
       try {
           return rolesService.add(httpServletRequest,role)==1?Result.success(1):Result.failure();
       }catch (Exception e){
           return Result.failure();
       }
    }
    @PostMapping("/getlroles")
    public Result<?>getlroles(Integer Page,Integer Number,Integer uStatus){
        List<?>list=rolesService.getlroles(Page,Number,uStatus);
        System.out.println(uStatus);
        System.out.println(list);
        return Result.success(list);
    }
    @PostMapping("/getroles")
    public Result<?>getroles(Integer rID){
        Roles roles=rolesService.getroles(rID);
        return roles==null?Result.failure():Result.success(roles);
    }
    @PostMapping("/getmyroles")
    public Result<?>getmyroles(HttpServletRequest httpServletRequest){
        Roles roles=rolesService.getmyroles(httpServletRequest);
        return roles==null?Result.failure():Result.success(roles);
    }
    @PostMapping("/delete")
    public Result<?>delete(Integer rID){
        return rolesService.delete(rID)==1?Result.success(1):Result.failure();
    }
}
